package com.hzeport.bgs22emduty.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzeport.bgs22emduty.config.login.UserInfoDetailVO;
import com.hzeport.bgs22emduty.dto.ScreenCheckQuestionsDTO;
import com.hzeport.bgs22emduty.entity.ScreenDutyFile;
import com.hzeport.bgs22emduty.entity.ScreenNotice;
import com.hzeport.bgs22emduty.entity.ScreenStandard;
import com.hzeport.bgs22emduty.enums.DeleteFlagEnum;
import com.hzeport.bgs22emduty.outbound.ScreenCheckQuestionsRepository;
import com.hzeport.bgs22emduty.vo.ScreenCheckQuestions.ScreenCheckQuestionsSaveVO;
import com.hzeport.bgs22emduty.common.base.BaseEntity;
import com.hzeport.bgs22emduty.common.base.BaseResult;
import com.hzeport.bgs22emduty.common.base.BaseServiceImpl;
import com.hzeport.bgs22emduty.entity.ScreenCheckQuestions;
import com.hzeport.bgs22emduty.outbound.db.ScreenCheckQuestionsRepositoryImpl;
import com.hzeport.bgs22emduty.service.ScreenCheckQuestionsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * ${comments}
 *
 * @author code generator
 * @date 2023-02-14 09:39:44
 */
@Service
@Slf4j
public class ScreenCheckQuestionsServiceImpl extends BaseServiceImpl<ScreenCheckQuestionsRepositoryImpl, ScreenCheckQuestions> implements ScreenCheckQuestionsService {

    @Resource
    private ScreenCheckQuestionsRepository screenCheckQuestionsRepository;

    @Override
    public BaseResult<Page<ScreenCheckQuestions>> list(ScreenCheckQuestionsDTO dto, UserInfoDetailVO userInfoDetailVO) {
        LambdaQueryWrapper<ScreenCheckQuestions> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(userInfoDetailVO.getCustomscode()), ScreenCheckQuestions::getCustomId, userInfoDetailVO.getCustomscode());
        if(!CollectionUtils.isEmpty(dto.getData())&&dto.getData().get(0)!=null){
            queryWrapper.ge(ScreenCheckQuestions::getCreateTime, LocalDateTime.parse(dto.getData().get(0)+"T00:00:00"));
        }
        if(!CollectionUtils.isEmpty(dto.getData())&&dto.getData().get(1)!=null){
            queryWrapper.le(ScreenCheckQuestions::getCreateTime, LocalDateTime.parse(dto.getData().get(1)+"T23:59:59"));
        }
        if(!StringUtils.isEmpty(dto.getQuestions())){
            queryWrapper.and(lamb->lamb.like(ScreenCheckQuestions::getTitle,dto.getQuestions()).or().like(ScreenCheckQuestions::getMainBody,dto.getQuestions()));
        }
        queryWrapper.eq(StringUtils.isNotBlank(userInfoDetailVO.getCustomscode()), ScreenCheckQuestions::getCustomId,userInfoDetailVO.getCustomscode());
        queryWrapper.orderByDesc(BaseEntity::getCreateTime);
        Page<ScreenCheckQuestions> page = new Page<>(dto.getPageNum(),dto.getPageSize());
        Page<ScreenCheckQuestions> standardPage = screenCheckQuestionsRepository.page(page, queryWrapper);
        return BaseResult.ok(standardPage,"查询成功");
    }

    @Override
    public BaseResult delete(String id, UserInfoDetailVO userInfoDetailVO) {
        screenCheckQuestionsRepository.update(new LambdaUpdateWrapper<ScreenCheckQuestions>()
                .eq(ScreenCheckQuestions::getId,id)
                .set(ScreenCheckQuestions::getDelFlag, DeleteFlagEnum.IS_DELETE.getValue())
                .set(ScreenCheckQuestions::getUpdateBy,userInfoDetailVO.getUserId())
                .set(ScreenCheckQuestions::getUpdateTime, LocalDateTime.now()));
        return BaseResult.ok(null,"删除成功");
    }

    @Override
    public BaseResult<ScreenCheckQuestions> detail(String id) {
        ScreenCheckQuestions byId = screenCheckQuestionsRepository.getById(id);
        return BaseResult.ok(byId,"查看成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult saveData(ScreenCheckQuestionsSaveVO screenCheckQuestionsSaveVO, UserInfoDetailVO userInfoDetailVO) {
        ScreenCheckQuestions screenCheckQuestions = new ScreenCheckQuestions();
        BeanUtils.copyProperties(screenCheckQuestionsSaveVO,screenCheckQuestions);
        if(StringUtils.isEmpty(screenCheckQuestionsSaveVO.getId())){
            screenCheckQuestions.setCustomId(userInfoDetailVO.getCustomscode());
            screenCheckQuestions.setCustomName(userInfoDetailVO.getCustomsName());
            screenCheckQuestions.setCreatePerson(userInfoDetailVO.getUserName());
            screenCheckQuestions.setCreateBy(userInfoDetailVO.getUserId());
            screenCheckQuestions.setCreateTime(LocalDateTime.now());
            screenCheckQuestions.setUpdateBy(userInfoDetailVO.getUserId());
            screenCheckQuestions.setUpdateTime(LocalDateTime.now());
            screenCheckQuestions.setDelFlag(DeleteFlagEnum.NOT_DELETE.getValue());
            screenCheckQuestionsRepository.save(screenCheckQuestions);
        }else {
            ScreenCheckQuestions byId = screenCheckQuestionsRepository.getById(screenCheckQuestionsSaveVO.getId());

            screenCheckQuestions.setCustomId(userInfoDetailVO.getCustomscode());
            screenCheckQuestions.setCustomName(userInfoDetailVO.getCustomsName());
            screenCheckQuestions.setUpdateBy(userInfoDetailVO.getUserId());
            screenCheckQuestions.setUpdateTime(LocalDateTime.now());
            screenCheckQuestions.setCreatePerson(userInfoDetailVO.getUserName());
            screenCheckQuestions.setDelFlag(DeleteFlagEnum.NOT_DELETE.getValue());
            if (byId != null){
                screenCheckQuestions.setRecVersion(byId.getRecVersion());
            }
            screenCheckQuestionsRepository.updateById(screenCheckQuestions);
        }
        return BaseResult.ok(screenCheckQuestions,"保存成功");
    }
}
